<?php
/*
 *   This file is part of verbum.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * 	 Copyright (c) 2008 Sergio Gabriel Teves <gabriel.sgt at gmail.com>
 */

$path = str_replace('$LANG',$team->lang,$prj->path);
$path .= str_replace('$LANG',$team->lang,$subprj->path);

$_log->debug("Exploring ".SVN_ROOT." - ".$path);

$svn  = new phpsvnclient;
$svn->setRespository(SVN_ROOT);

$svnlist = $svn->getDirectoryFiles($path);

/* FOR TESTING	ONLY 
	$ar = array("path"=>"trunk/archivonuevo4.es.po");
	$ar1 = array("path"=>"trunk/archivonuevo2.es.po");
	$ar2 = array("path"=>"trunk/archivonuevo3.es.po");
	
	$svnlist = array($ar,$ar1,$ar2);
	*/
$deletefiles = array();
$newfiles = array();

$_log->debug("Response count: ".count($svnlist));

if (count($svnlist)>0) {
	if (!$update) {
		$_log->debug("CREATE MODE");
		$h=new history($prj->id);
		$loader->execute($h->cleanTeam($subprj->id,$team->id));
		$ah=$db->affected_rows();

		$f=new files($prj->id);
		$loader->execute($f->deleteTeam($subprj->id,$team->id));
		$af=$db->affected_rows();
		if ($af>0) {
			$_log->debug("Deleted ".$ah." existing history rows.");
			$_log->debug("Deleted ".$af." existing file rows.");	
		}			
	} else {
		$_log->debug("UPDATE MODE");
	}
}

$saved=0;
$data = array();
foreach ($svnlist as $key => $obj) {
	$f = $obj['path'];
	$fname = substr($f,strrpos($f,'/')+1);
	$fext = substr($fname,strrpos($fname,".")+1);
	if ($fext=="po") {
		array_push($data,$fname);
		$file = new files($prj->id);
		$file->teamid=$team->id;
		$file->name=$fname;
		$file->prjid=$subprj->id;
		
		if ($update) {
			if ($loader->getBy($file,$file->findFile($subprj->id))===false) {
				$_log->debug("Added ".$file->name);
				$loader->save($file);
				array_push($newfiles,$file);
				$saved +=1;	
			} else {
				if ($file->fstatus=="1") {
					$_log->debug("Restored ".$file->name);
					$file->fstatus="0";
					$loader->save($file);
					array_push($newfiles,$file);
					$saved +=1;	
				}
			}
		} else {
			$loader->save($file);
			$saved +=1;
			array_push($newfiles,$file);
		}
	} else {
		$_log->warn("Invalid file ".$fname);
	}
	
}

$del=0;

if ($update) {
	$file = new files($prj->id);
	$file->teamid=$team->id;
	$file->prjid=$subprj->id;
	$list = $loader->loadListBy($file,$file->loadfiltered());

	foreach ($list as $obj) {
		if (in_array($obj->name,$data)===false) {
			/*
			$h = new history($prj->id);
			$h->fileid=$obj->id;
			$loader->delete($h);
			$loader->delete($obj);
			*/
			$del+=1;
			$obj->fstatus="1";
			$loader->update($obj);
			$deletefiles[]=$obj;
			$_log->info("Remove: ".$obj->name);
		}
	}
	
}

$_log->info("Processed ".$team->name." - ".$saved." files created.");

if ($del>0) {
	$_log->info($del." files should be removed.");
}
 
?>
